package com.mobiletrain.org.motherkitch.sliding;

import android.content.Intent;
import android.support.v7.app.ActionBar;
import android.support.v7.app.ActionBarActivity;
import android.os.Bundle;
import android.support.v7.widget.Toolbar;
import android.view.Menu;
import android.view.MenuItem;
import android.view.View;
import android.view.ViewGroup;
import android.view.animation.Animation;
import android.view.animation.AnimationUtils;
import android.view.animation.LinearInterpolator;
import android.widget.AdapterView;
import android.widget.BaseAdapter;
import android.widget.ImageView;
import android.widget.ListView;
import android.widget.ProgressBar;
import android.widget.TextView;
import android.widget.Toast;

import com.lidroid.xutils.BitmapUtils;
import com.lidroid.xutils.DbUtils;
import com.lidroid.xutils.exception.DbException;
import com.mobiletrain.org.motherkitch.MyApplication;
import com.mobiletrain.org.motherkitch.R;
import com.mobiletrain.org.motherkitch.fenlei.ThridActivity;
import com.mobiletrain.org.motherkitch.fenlei.bean.ShouCang;
import com.mobiletrain.org.motherkitch.utils.ImageLoad;

import java.util.ArrayList;
import java.util.List;

public class CollectActivity extends ActionBarActivity {

    private Toolbar toolbar;
    private ListView mCollect;
    private ProgressBar empty;
    private List<ShouCang> shous;
    private BitmapUtils bitmapUtils;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_collect);
        getURLfromDB();
        findView();
        toolBarMth();



    }

    public void getURLfromDB(){
        DbUtils dbUtils = ((MyApplication) getApplication()).getDbUtils();
        try {
            shous = dbUtils.findAll(ShouCang.class);

        } catch (DbException e) {
            e.printStackTrace();
        }
    }

    class MyAdapter extends BaseAdapter{

        @Override
        public int getCount() {
            if(shous!=null){
                return shous.size();
            }
            return 0;
        }

        @Override
        public Object getItem(int position) {
            return shous.get(position);
        }

        @Override
        public long getItemId(int position) {
            return position;
        }

        @Override
        public View getView(int position, View convertView, ViewGroup parent) {
            MyHo hoder=null;
            if(convertView==null){
                hoder=new MyHo();
                convertView=getLayoutInflater().inflate(R.layout.secondlist_item,null);
                hoder.icon= (ImageView) convertView.findViewById(R.id.second_list_item_icon);
                hoder.title= (TextView) convertView.findViewById(R.id.second_list_item_title);
                hoder.foot= (TextView) convertView.findViewById(R.id.second_list_item_foot);
                hoder.num= (TextView) convertView.findViewById(R.id.second_list_item_nul);
                convertView.setTag(hoder);
            }else{
                hoder= (MyHo) convertView.getTag();
            }
            ShouCang sou=shous.get(position);
            bitmapUtils.display(hoder.icon,sou.getIcon());
            hoder.title.setText(sou.getTitle());
            hoder.foot.setText(sou.getCai());
            hoder.num.setText(sou.getNum());
            return convertView;
        }
        class MyHo{
            ImageView icon;
            TextView title;
            TextView foot;
            TextView num;

        }
    }
    public void findView() {
        toolbar = (Toolbar) findViewById(R.id.coll_title);
        mCollect = (ListView) findViewById(R.id.coll_list);
        empty = (ProgressBar) findViewById(R.id.empty);
        bitmapUtils = ImageLoad.getBitmapUtils(this);
        Animation animation =
                AnimationUtils.loadAnimation(
                        CollectActivity.this, R.anim.progress_anim);
        LinearInterpolator lir = new LinearInterpolator();
        animation.setInterpolator(lir);
        empty.setAnimation(animation);
        empty.startAnimation(animation);
        mCollect.setEmptyView(empty);
        mCollect.setAdapter(new MyAdapter());
        mCollect.setOnItemClickListener(new AdapterView.OnItemClickListener() {
            @Override
            public void onItemClick(AdapterView<?> parent, View view, int position, long id) {
                Intent i=new Intent();
                i.setClass(CollectActivity.this,ThridActivity.class);
                Bundle bundle1=new Bundle();
                bundle1.putString("id",shous.get(position).getId());
                i.putExtra("key",bundle1);
                startActivity(i);
            }
        });
    }


    public void toolBarMth() {
        setSupportActionBar(toolbar);
        toolbar.setNavigationIcon(R.drawable.back);
        toolbar.setNavigationOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                CollectActivity.this.finish();
            }
        });

    }

    @Override
    public boolean onCreateOptionsMenu(Menu menu) {
        // Inflate the menu; this adds items to the action bar if it is present.
        getMenuInflater().inflate(R.menu.menu_collect, menu);
        return true;
    }

    @Override
    public boolean onOptionsItemSelected(MenuItem item) {
        // Handle action bar item clicks here. The action bar will
        // automatically handle clicks on the Home/Up button, so long
        // as you specify a parent activity in AndroidManifest.xml.
        int id = item.getItemId();

        //noinspection SimplifiableIfStatement
        if (id == R.id.action_settings) {
            return true;
        }

        return super.onOptionsItemSelected(item);
    }
}
